import Vue from "vue";
import Vuex from "vuex";
import {logout} from '../api/user'

Vue.use(Vuex);

const user = JSON.parse(window.localStorage.getItem('setUser'));

export default new Vuex.Store({
    state: {
        user:user,
        token:''
    },
    getters:{
        getUser(state){
            return state.user
        },
        getToken(state){
            return state.token
        }
    },
    mutations: {
        SET_USER(state,user) {
            state.user = user;
        },
        SET_TOKEN(state,token) {
            state.token = token;
        }
    },
    actions: {
        setUser({commit},user){
            return new Promise((resolve, reject) => {
                commit('SET_USER',user)
                resolve()
            })
        },
        setToken({commit},token){
            commit('SET_TOKEN',token)
        },
        logout({ commit, state }){
            return new Promise((resolve, reject) => {
                logout().then(() => {
                  commit('SET_TOKEN', '')
                  //commit('SET_ROLES', []) 暂时没有
                  resolve()
                }).catch(error => {
                  reject(error)
                })
              })
        }
    }
});
